﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.world.machines {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.models.world.vos.actors.machines.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.controllers.startup.*;
    import net.wooga.pets.views.components.controls.*;

    public class TreatmentMachineMediator extends AbstractMachineMediator {

		[Inject]
        public var tutorialModel:TutorialModel;
		[Inject]
        public var energyModel:EnergyModel;
		[Inject]
        public var gameViewModel:GameViewModel;
        protected var _clockAnimation:MovieClip;
        protected var _boostAnimationTimer:Timer;
        protected var _oldTreatmentTime:int;
        protected var _newTreatmentTime:int;
        protected var _bubbleHeight:Number;// = 0
        protected var _textField:TextField;
        protected var _textBubble:MovieClip;
        protected var _boostButton:MovieClip;
        protected var _boostButtonType:String = "Icons_BoostIconButton"
        protected var _textAnimation:MovieClip;

        private static const BUTTON_FONTSIZE:int = 21;
        private static const BUTTON_FONTCOLOR:String = "0xffffff";
        private static const TIME_FONTSIZE:int = 60;
        private static const TIME_COLOR:String = "#f94034";
        private static const TEXT_FONTSIZE:int = 60;
        private static const TEXT_COLOR:String = "0x663300";
        private static const TEXTANIMATION_FONTSIZE:int = 54;
        private static const TEXTANIMATION_FONTCOLOR:String = "0xFFE046";

        protected function get view():TreatmentMachineView{
            return ((viewComponent as TreatmentMachineView));
        }
        protected function get vo():TreatmentMachineVO{
            return ((_vo as TreatmentMachineVO));
        }
        override public function onRegister():void{
            super.onRegister();
            this._textBubble = AssetsLibrary.getMovieClip(SpeechBubbleConstants.MACHINE_CUSTOM_BUBBLE);
            this._boostButton = AssetsLibrary.getMovieClip(this._boostButtonType);
            this.setTextField(this._boostButton.button.label, "global_boost", "MyriadProFont", BUTTON_FONTCOLOR, BUTTON_FONTSIZE);
            this.updateActor(null);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_TREATMENT_STARTED, this.checkIfMouseOverMachine);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_TREATMENT_FINISHED, this.killMouseOverBubble);
            this.view.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOver);
        }
        override protected function updateActor(_arg1:Object):void{
            super.updateActor(_arg1);
            var _local2:String = (this.vo.patient) ? this.vo.patient.petDisease : "empty";
            this.view.setAnimationState(_local2);
        }
        override public function onTick(_arg1:Boolean=true, _arg2:Number=0):void{
            var _local3:String;
            if (this.vo.treatmentTimeLeft > 0){
                _local3 = (((((languageModel.getTextByKey("global_ready_in") + "<font size='") + TIME_FONTSIZE) + "' color='") + TIME_COLOR) + "'><br/>[[time_until_ready]]</font>");
                _local3 = _local3.replace("[[time_until_ready]]", TimeUtils.secondsToHoursMinSec(this.vo.treatmentTimeLeft));
                this.vo.showSpeechBubble(SpeechBubbleConstants.TYPE_ENERGY_BUBBLE, {text:_local3});
            } else {
                dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
                this.stopTextAnimation();
            };
        }
        override protected function onMouseOut(_arg1:MouseEvent):void{
            dispatch(new InfoEvent(EventConstants.CHECK_REMOVE_SPEECHBUBBLE_FROM_LAYER, null, {component:this, vo:this.vo}));
        }
        override protected function onRemoveMouseOver(_arg1:InfoEvent):void{
            if (_arg1.id == id){
                super.onRemoveMouseOver(_arg1);
                this.stopTextAnimation();
            };
        }
        protected function get isBoostButtonAvailable():Boolean{
            var _local1 = (this.vo.state == WorldConstants.WORKING);
            _local1 = ((_local1) && ((this.tutorialModel.state >= TutorialModel.LEVEL1_BOOST_PATIENT)));
            return (_local1);
        }
        override protected function onMouseOver(_arg1:MouseEvent):void{
            if (this.isBoostButtonAvailable){
                dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
            } else {
                dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
                dispatch(new InfoEvent(EventConstants.REMOVE_SPEECHBUBBLE_FROM_LAYER, null));
            };
            super.onMouseOver(_arg1);
        }
        override protected function onClick(_arg1:MouseEvent):void{
            switch (this.vo.state){
                case WorldConstants.WORKING:
                    this.handleBoosting();
                    break;
                case WorldConstants.DIRTY:
                    this.view.startCleaningAnimation();
                    dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_CLEAN_MACHINE));
                    break;
                case WorldConstants.CHARGED:
                    this.vo.checkForDoctorFeedHint();
                    break;
            };
            super.onClick(_arg1);
        }
        private function handleBoosting():void{
            var _local1:int = this.energyModel.pendingCallsToBackendCount;
            var _local2 = (this.vo.patient.currentTreatment.finishedIn > (_local1 * this.energyModel.boostDuration));
            var _local3 = (this.energyModel.energy > 0);
            if (((_local3) && (_local2))){
                dispatch(new InfoEvent(EventConstants.BOOST_MACHINE, this.vo.patient.currentTreatment.id));
                this.playBoostAnimation();
                this.createRewardAnimation(this.vo.id);
            } else {
                if (((!(_local3)) && (_local2))){
                    l("---- ADD MORE");
                    this.stopTextAnimation();
                    dispatch(new InfoEvent(EventConstants.REMOVE_SPEECHBUBBLE_FROM_LAYER));
                    dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.OUT_OF_ENERGY_POPUP));
                };
            };
            this.updateBoostButton();
        }
        private function createRewardAnimation(_arg1:String):void{
            var _local2:Object = new Object();
            _local2[EventConstants.ITEM_TYPE] = GameConstants.REWARD_BOOST;
            _local2[EventConstants.TARGET_ID] = _arg1;
            dispatch(new InfoEvent(EventConstants.CREATE_REWARD_ANIMATION_INTO_HOSPITAL, null, _local2));
        }
        protected function playBoostAnimation():void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
            if (this._clockAnimation){
                this._clockAnimation.gotoAndPlay("tick");
            };
            if (this._textAnimation){
                this._textAnimation.gotoAndPlay("start");
            };
            this.view.startBoostAnimation();
            this.stopTextAnimation();
            this._oldTreatmentTime = (this.vo.patient.currentTreatment.finishedIn - ((this.energyModel.pendingCallsToBackendCount - 1) * this.energyModel.boostDuration));
            this._newTreatmentTime = (this.vo.patient.currentTreatment.finishedIn - (this.energyModel.pendingCallsToBackendCount * this.energyModel.boostDuration));
            this._boostAnimationTimer = new Timer(50, 20);
            this._boostAnimationTimer.addEventListener(TimerEvent.TIMER, this.timeAnimationTick);
            this._boostAnimationTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.timeAnimationComplete);
            this._boostAnimationTimer.start();
        }
        private function stopTextAnimation():void{
            if (((this._boostAnimationTimer) && (this._boostAnimationTimer.running))){
                this._boostAnimationTimer.removeEventListener(TimerEvent.TIMER, this.timeAnimationTick);
                this._boostAnimationTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.timeAnimationComplete);
                this._boostAnimationTimer.stop();
            };
        }
        private function timeAnimationTick(_arg1:TimerEvent):void{
            dispatch(new TickingEvent(TickingEvent.REMOVE_TICKING, this));
            var _local2:int = (this._oldTreatmentTime - ((this.energyModel.boostDuration / this._boostAnimationTimer.repeatCount) * this._boostAnimationTimer.currentCount));
            this.setTimeForEnergyBubble(Math.max(_local2, 0));
            this.setEnergyBubble();
        }
        private function timeAnimationComplete(_arg1:TimerEvent):void{
            this.setTimeForEnergyBubble(Math.max(this._newTreatmentTime, 0));
            dispatch(new TickingEvent(TickingEvent.ADD_TICKING, this, TimeConstants.SECOND));
            this.stopTextAnimation();
            this.setEnergyBubble();
        }
        override protected function setEnergyBubble():void{
            var _local4:Point;
            this._textField = ((this._textField) || (new SpeechBubbleTextfield(TEXT_COLOR, TEXT_FONTSIZE)));
            this._clockAnimation = ((this._clockAnimation) || (AssetsLibrary.getMovieClip("Icons_ClockAnimation")));
            this._textAnimation = ((this._textAnimation) || (AssetsLibrary.getMovieClip("Icons_BoostTextAnimation")));
            var _local1:ISpeaking = (_vo as ISpeaking);
            var _local2:Object = _local1.speechBubbleData;
            if (((!(this._boostAnimationTimer)) || (!(this._boostAnimationTimer.running)))){
                this._textField.htmlText = ((_local2.text) || (""));
            };
            this.renderBubble();
            this.view.speechBubblePosition = this.vo.speechBubblePosition;
            if (_local2.position){
                _local4 = new Point(_local2.position.x, _local2.position.y);
                this.view.speechBubblePosition = _local4;
            };
            var _local3:Point = this.view.localToGlobal(this.view.speechBubblePosition);
            this.view.mouseOverEffect();
            if (((((((this.vo) && (this.vo.patient))) && (this.vo.patient.currentTreatment))) && ((this.vo.patient.currentTreatment.finishedIn > 0)))){
                dispatch(new InfoEvent(EventConstants.ADD_SPEECHBUBBLE_TO_LAYER, null, {mc:this._textBubble, pos:_local3, mouseOverEffect:this.view.getBitmapFilter(), zoom:true}));
            };
        }
        override protected function setBoostBubble():void{
            var _local5:Point;
            var _local1:ISpeaking = (_vo as ISpeaking);
            var _local2:Object = _local1.speechBubbleData;
            this.view.speechBubblePosition = this.vo.speechBubblePosition;
            if (_local2.position){
                _local5 = new Point(_local2.position.x, _local2.position.y);
                this.view.speechBubblePosition = _local5;
            };
            var _local3:MovieClip = AssetsLibrary.getMovieClip(this._boostButtonType);
            this.setTextField(_local3.button.label, _local2.key, "MyriadProFont", BUTTON_FONTCOLOR, BUTTON_FONTSIZE);
            _local3.scaleX = (_local3.scaleY = 2.5);
            var _local4:MovieClip = AssetsLibrary.getMovieClip(SpeechBubbleConstants.MACHINE_CUSTOM_BUBBLE);
            _local4.background.width = ((SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_LEFT + _local3.width) + SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_RIGHT);
            _local4.background.height = (((SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_BOTTOM + _local3.height) + 20) + SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_TOP);
            _local3.x = (((-(_local4.background.width) / 2) - (_local3.width / 2)) + SpeechBubbleConstants.MACHINE_BUBBLE_OFFSET_RIGHT);
            _local3.y = ((_local4.background.y - _local3.height) + SpeechBubbleConstants.MACHINE_BUBBLE_OFFSET_BOTTOM);
            _local4.addChild(_local3);
            _local4.scaleX = (_local4.scaleY = (1.5 / this.gameViewModel.gameViewScale));
            this.view.addSpeechBubble(_local4);
        }
        private function renderBubble():void{
            this._boostButton.scaleX = (this._boostButton.scaleY = 2.5);
            this._clockAnimation.scaleX = (this._clockAnimation.scaleY = 2.5);
            var _local1:Number = Math.max(this._textField.width, this._boostButton.width);
            var _local2:Number = Math.max(this._bubbleHeight, (this._textField.height + this._boostButton.height));
            this._textBubble.background.width = ((SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_LEFT + _local1) + SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_RIGHT);
            this._textBubble.background.height = ((SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_BOTTOM + _local2) + SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_TOP);
            this._textField.x = (((-(this._textBubble.background.width) / 2) - (this._textField.width / 2)) + SpeechBubbleConstants.MACHINE_BUBBLE_OFFSET_RIGHT);
            this._textField.x = Math.round(this._textField.x);
            this._textField.y = ((-(this._textBubble.background.height) + SpeechBubbleConstants.MACHINE_BUBBLE_MARGIN_TOP) + SpeechBubbleConstants.MACHINE_BUBBLE_OFFSET_BOTTOM);
            this._textField.y = Math.round(this._textField.y);
            this._boostButton.x = (((-(this._textBubble.background.width) / 2) - (this._boostButton.width / 2)) + SpeechBubbleConstants.MACHINE_BUBBLE_OFFSET_RIGHT);
            this._boostButton.y = ((this._textBubble.background.y - _local2) + this._boostButton.height);
            this.updateBoostButton();
            this._clockAnimation.x = (this._textBubble.background.x + SpeechBubbleConstants.MACHINE_BUBBLE_CLOCK_X);
            this._clockAnimation.y = (this._textBubble.background.y + SpeechBubbleConstants.MACHINE_BUBBLE_CLOCK_Y);
            this.setTextField(this._textAnimation.animationText.tf, "global_amount_boosted_time", "ShowcardGothic", TEXTANIMATION_FONTCOLOR, TEXTANIMATION_FONTSIZE);
            this._textAnimation.x = 0;
            this._textAnimation.y = (0 + SpeechBubbleConstants.MACHINE_BUBBLE_TEXTANIMATION_Y);
            this._textBubble.addChild(this._textField);
            this._textBubble.addChild(this._boostButton);
            this._textBubble.addChild(this._clockAnimation);
            this._textBubble.addChild(this._textAnimation);
        }
        protected function updateBoostButton():void{
            if (this.energyModel.energy == 0){
                this._boostButton.button.gotoAndStop("add_more");
                this.setTextField(this._boostButton.button.label, "meta_energy_button2", "MyriadProFont", BUTTON_FONTCOLOR, BUTTON_FONTSIZE);
            } else {
                this._boostButton.button.gotoAndStop("boost");
                this.setTextField(this._boostButton.button.label, "global_boost", "MyriadProFont", BUTTON_FONTCOLOR, BUTTON_FONTSIZE);
            };
        }
        protected function setTimeForEnergyBubble(_arg1:int):void{
            var _local2:String = TimeUtils.secondsToHoursMinSec(_arg1);
            var _local3:String = languageModel.getTextByKey("global_ready_in");
            _local3 = (((((((_local3 + "<font size='") + TIME_FONTSIZE) + "' color='") + TIME_COLOR) + "'><br/>") + _local2) + "</font>");
            if (this._textField){
                this._textField.htmlText = _local3;
            };
        }
        private function checkIfMouseOverMachine(_arg1:Event):void{
            var _local2:Boolean;
            if (((this.view) && (this.view.stage))){
                _local2 = this.view.hitTestPoint(this.view.stage.mouseX, this.view.stage.mouseY);
            };
            if (_local2){
                this.onMouseOver(new MouseEvent(MouseEvent.MOUSE_OVER));
            };
        }
        protected function killMouseOverBubble(_arg1:InfoEvent):void{
            if (((this.vo.patient) && ((_arg1.id == this.vo.patient.id)))){
                dispatch(new InfoEvent(EventConstants.REMOVE_SPEECHBUBBLE_FROM_LAYER, null));
            };
        }
        protected function setTextField(_arg1:TextField, _arg2:String, _arg3:String, _arg4:String, _arg5:int):void{
            var _local6:String = languageModel.getTextByKey(_arg2);
            var _local7:TextFormat = new TextFormat();
            _local7.font = _arg3;
            _local7.align = TextFormatAlign.CENTER;
            _local7.bold = true;
            _local7.color = _arg4;
            _local7.size = _arg5;
            _arg1.embedFonts = true;
            _arg1.htmlText = _local6;
            _arg1.autoSize = TextFieldAutoSize.CENTER;
            _arg1.multiline = false;
            _arg1.setTextFormat(_local7);
        }

    }
}//package net.wooga.pets.views.world.machines 
